﻿using UnityEngine;

namespace UnityView
{
    public class UIToastInfo
    {
        public string Text;
        public UIRect Rect = UIToast.DefaultRect;
        public float ShowTime = UIToast.DefaultShowTime;
        public float FadeTime = UIToast.DefaultFadeTime;
        public bool Fade = true;
        public bool ShowImmediately = true;

        public Color BackgroundColor = UIToast.DefaultBackgroundColor;
        public Color TextColor = UIToast.DefaultTextColor;

        public float LifeTime = UIToast.DefaultShowTime + UIToast.DefaultFadeTime;

        public UIToastInfo(string text)
        {
            Text = text;
        }

        public UIToastInfo(string text, float showTime)
        {
            Text = text;
            ShowTime = showTime;
        }

        public UIToastInfo(string text, float showTime, float fadeTime)
        {
            Text = text;
            ShowTime = showTime;
            if (fadeTime < 0.01f)
            {
                Fade = false;
            }
            else
            {
                FadeTime = fadeTime;
            }
        }

        public UIToastInfo(string text, float showTime, bool fade)
        {
            Text = text;
            ShowTime = showTime;
            Fade = fade;
        }

        public UIToastInfo(string text, UIRect rect)
        {
            Text = text;
            Rect = rect;
        }

        public UIToastInfo(string text, UIRect rect, float showTime, float fadeTime, bool immediately)
        {
            Text = text;
            ShowTime = showTime;
            FadeTime = fadeTime;
            Rect = rect;
            ShowImmediately = immediately;
            LifeTime = ShowTime + FadeTime;
        }
    }

    public class UIToast : ButtonView
    {
        public static float DefaultShowTime = 0.7f;
        public static float DefaultFadeTime = 0.25f;
        public static UIRect DefaultRect = new UIRect(0.32f, 0.68f, 0.15f, 0.26f);
        public static Color DefaultBackgroundColor = Color.white;
        public static Color DefaultTextColor = Color.black;
        public static int DefaultTextSize = 50;

        public static Color AppearanceBackgroundColor = new Color(0, 0, 0, 0.7f);
        public static Color AppearanceTextColor = Color.white;

        public UIToast() : base("UI Toast")
        {
            UIObject.SetActive(false);
            Title = "";
            Update();
        }

        public void Update()
        {
            UIRect = DefaultRect;
            BackgroundColor = DefaultBackgroundColor;
            TitleColor = DefaultTextColor;
            TitleLabel.FontSize = DefaultTextSize;
        }

        public static void Show(string text)
        {
            Show(new UIToastInfo(text));
        }

        public static void Show(string text, float showTime)
        {
            Show(new UIToastInfo(text, showTime));
        }
        public static void Show(string text, float showTime, float fadeTime)
        {
            Show(new UIToastInfo(text, showTime, fadeTime));
        }
        public static void Show(string text, float showTime, bool fade)
        {
            Show(new UIToastInfo(text, showTime, fade));
        }

        public static void Show(string text, UIRect rect)
        {
            Show(new UIToastInfo(text, rect));
        }

        public static void Show(UIToastInfo toastInfo)
        {
            UIViewManager.Instance.ShowToast(toastInfo);
        }

        // 更新一次时间状态，返回值为该Toast的生命是否结束
        // public bool UpdateLifeTime(float delta)
        // {
        //     LifeTick += delta;
        //     if (LifeTick < FadeTime)
        //     {
        //         BackgroundColor = new Color(OriginalBackgroundColor.r, OriginalBackgroundColor.g, OriginalBackgroundColor.b,
        //             LifeTick / FadeTime * OriginalBackgroundColor.a);
        //         TitleText.color = new Color(OriginalTextColor.r, OriginalTextColor.g, OriginalTextColor.b, LifeTick / FadeTime * OriginalTextColor.a);
        //     }
        //     else if (LifeTime - LifeTick < FadeTime)
        //     {
        //         float alpha = (LifeTime - LifeTick) / FadeTime * OriginalBackgroundColor.a;
        //         BackgroundColor = new Color(OriginalBackgroundColor.r, OriginalBackgroundColor.g, OriginalBackgroundColor.b,
        //             alpha);
        //         TitleText.color = new Color(OriginalTextColor.r, OriginalTextColor.g, OriginalTextColor.b, alpha);
        //     }
        //     else
        //     {
        //         BackgroundColor = OriginalBackgroundColor;
        //         TitleText.color = OriginalTextColor;
        //     }
        //
        //     LifeTick += delta;
        //     if (LifeTick < LifeTime) return false;
        //     Destroy();
        //     return true;
        // }
    }
}
